package com.xhtt.modules.inspection.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhtt.modules.inspection.dao.InsTaskDateMapper;
import com.xhtt.modules.inspection.entity.InsTask;
import com.xhtt.modules.inspection.entity.InsTaskDate;
import com.xhtt.modules.inspection.service.IInsTaskDateService;
import com.xhtt.modules.inspection.service.IInsTaskUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 巡检任务 服务实现类
 * </p>
 *
 * @author xj
 * @since 2019-01-29
 */
@Service
public class InsTaskDateServiceImpl extends ServiceImpl<InsTaskDateMapper, InsTaskDate> implements IInsTaskDateService {

    @Autowired
    private IInsTaskUserService insTaskUserService;

    public void addDateAndUser(List<LocalDateTime> taskDays, InsTask insTask, List<Integer> users) {


        Integer taskId = insTask.getId();

        // 保存任务的子表-时间表  一天对应一条记录
        InsTaskDate insTaskDate;
        List<InsTaskDate> insTaskDates = new ArrayList<>();
        for (LocalDateTime taskDay : taskDays) {
            insTaskDate = new InsTaskDate();
            BeanUtils.copyProperties(insTask, insTaskDate);
            insTaskDate.setTaskId(taskId);
            insTaskDate.setDateTime(taskDay);
            insTaskDates.add(insTaskDate);
        }
        this.saveBatch(insTaskDates);
        
        // 保存时间的子表-人员表  一条记录对应多个人
        insTaskDates.forEach(insTaskDate1 -> insTaskUserService.add(users, insTaskDate1));


    }

}
